﻿import { ModuleC, ModuleS, SoundManager, UI } from "odin";
import { Consts } from "../../Consts";
import { UIGame } from "../../ui/UIGame";

/*
 * @Author: zhaolei
 * @Date: 2022-08-10 16:12:25
 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
 * @Description: 游戏模块，管理游戏周期，目前用来区别战斗中和平时的背景音效
 * 客户端-展示游戏主UI，管理背景音乐
 * 服务端-进入战斗时告诉客户端展示UI，更换背景音乐
 */
export class GameModuleS extends ModuleS<GameModuleC, null> {
	/**
	 * 怪物血量变化，由怪物模块调用
	 * @param monsterHp 怪物血量
	 */
	public onMonsterHpChange(monsterHp: number) {
		this.callWorldClientFun(this.client.net_OnMonsterHp(monsterHp));
	}

	/**
	 * 进入Boss战斗，UI上显示血条
	 */
	public enterBattle(player: GamePlay.Player) {
		// this.client是客户端模块GameModuleC的引用，
		// callClientFun会调用到客户端模块GameModuleC里的方法
		this.callClientFun(player, this.client.net_EnterBattle());
	}

	/**
	 * 将玩家重置到安全区域
	 */
	public net_rebirth() {
		this.currentPlayer.character.location = new Type.Vector(-3204.57, 3520, 180);
		this.currentPlayer.character.ragdoll(false);
	}

}
export class GameModuleC extends ModuleC<GameModuleS, null> {
	/**
	 * 场景死亡触发器
	 */
	private _deathTrigger: GamePlay.Trigger;

	/**
	 * 游戏UI
	 */
	private _gameUI: UIGame;

	public override onAwake(): void {
		UI.instance.showPanel(UIGame);
		// 通过odin框架获取UIGame实例，方便GameModuleC对象调用UIGame的方法
		this._gameUI = UI.instance.getPanel(UIGame);

		this._deathTrigger = MWCore.GameObject.find(Consts.DEATH_TRIGGER) as GamePlay.Trigger;
		this._deathTrigger.onEnter.add(gameObject => {
			//场景死亡trigger
			if (GamePlay.isCharacter(gameObject) && (gameObject as GamePlay.Character).player.getPlayerID() == this.currentPlayerId) {
				(gameObject as GamePlay.Character).ragdoll(true);
				setTimeout(() => {
					this.server.net_rebirth();
				}, 3000);
			}
		});
		this.quitBattle();
	}

	/**
	 * boss血量变化，更新UI
	 * @param monsterHp 怪物当前血量 
	 * (玩家当前血条)
	 */
	public net_OnMonsterHp(monsterHp: number): void {
		this._gameUI.updateMonsterHp(monsterHp);
	}

	/**
	 * 进入战斗,由服务端调用
	 */
	public net_EnterBattle() {
		SoundManager.instance.playBGM(Consts.BATTLE_BGM);
		this._gameUI.setBossHpView(true);
	}

	/**
	 * 退出战斗
	 */
	public quitBattle() {
		SoundManager.instance.playBGM(Consts.ROOM_BGM);
		this._gameUI.setBossHpView(false);
	}
}